<?php
    namespace app\listen\service;
    use app\listen\model\Member;
    use app\listen\model\Role;

    class MemberService {
        private $memberMod;
        private $roleMod;
        public function _initialize() {

        }
        
        // 查询用户是否存在
        public function findMemberByOpenId(string $openid) {
            // 检查游客是否存在
            $user = $this->getMemberModel()->findMemberByOpenId($openid);
            $user = $this->getRoles($user);
            return $user;
        }
        // 获取角色列表
        function getRoles($user) {
            // 查询角色列表
            if (!empty($user)) {
                $user['roles'] = $this->getRoleModel()->findRoleByuId($user['uid']);
            }
            return $user;
        }
        // 注册游客用户，无权限
        public function registVisitor(string $openid) {
            $data= [
                'nickname'=>'游客',
                'openid'=>$openid,
                'addtime' => request()->time(),
            ];
            $this->getMemberModel()->add($data);
            // 查询对应的用户
            $user = $this->getRoles($this->getMemberModel()->findMemberByOpenId($openid));
            return $user;
        }
        function  getMemberModel():Member {
            if (!$this->memberMod) {
                $this->memberMod = model('listen/Member');
            }
            return $this->memberMod;
        }

        function  getRoleModel():Role {
            if (!$this->roleMod) {
                $this->roleMod = model('listen/Role');
            }
            return $this->roleMod;
        }
    }